Use case: Take/Release a semaphore ¶
This use case shows how the application take a semaphore before accessing a shared resource, then releases it once the processing is completed.
Sequence diagram ¶
Code snippet ¶
|
HAL1 |
HAL2 |
|---|---|
uint32_t sem_state;
#define HSEM_SEM_ID (0U) /* HW semaphore 0*/
/* HSEM Clock Enable */
__HAL_RCC_HSEM_CLK_ENABLE() ;
HAL_HSEM_Take(HSEM_SEM_ID, 1U);
sem_state = HAL_HSEM_IsSemTaken(HSEM_SEM_ID);
if (sem_state == 1U)
{
/* The hardware semaphore is taken */
}
HAL_HSEM_Release(HSEM_SEM_ID, 1U);
sem_state = HAL_HSEM_IsSemTaken(HSEM_SEM_ID);
if (sem_state == 0U)
{
/* The hardware semaphore is released */
}
|
hal_hsem_status_t sem_state;
/* HSEM Clock Enable */
HAL_RCC_HSEM_EnableClock();
HAL_HSEM_Take(HAL_HSEM_0, 1U);
sem_state = HAL_HSEM_IsTaken(HAL_HSEM_0);
if (sem_state == HAL_HSEM_TAKEN)
{
/* The hardware semaphore is taken */
}
HAL_HSEM_Release(HAL_HSEM_0, 1U);
sem_state = HAL_HSEM_IsTaken(HAL_HSEM_0);
if (sem_state == HAL_HSEM_RELEASED)
{
/* The hardware semaphore is released */
}
|

